<template>
    <div class="crm-enterprise-container">
        <el-dialog title="修改企业" v-model="isShowDialog" :close-on-click-modal="false" >
            <el-form ref="editCrmEnterpriseFormRef" :model="formData" :rules="formRules" label-width="90px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="企业名称" prop="enterpriseName">
                            <el-input v-model="formData.enterpriseName" placeholder="请输入企业名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="统一社会信用代码" prop="creditCode" label-width="130px">
                            <el-input v-model="formData.creditCode" placeholder="请输入统一社会信用代码" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="法定代表人" prop="legalName">
                            <el-input v-model="formData.legalName" placeholder="请输入法定代表人" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="手机号码" prop="mobile">
                            <el-input v-model="formData.mobile" placeholder="请输入手机号码" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="电话号码" prop="telephone">
                            <el-input v-model="formData.telephone" placeholder="请输入电话号码" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="邮箱号码" prop="email">
                            <el-input v-model="formData.email" placeholder="请输入邮箱号码" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="官网地址" prop="website">
                            <el-input v-model="formData.website" placeholder="请输入官网地址" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="国标行业" prop="industry">
                            <el-input v-model="formData.industry" placeholder="请输入国标行业" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="详细地址" prop="address">
                            <el-input v-model="formData.address" placeholder="请输入详细地址" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="关联客户" prop="enterprise">
                            <el-select v-model="formData.customerIds" multiple filterable remote 
                                @click="loadCustomerData()"
                                :remote-method="loadCustomerData" placeholder="请选择关联客户" clearable class="w100">
								<el-option 
									v-for="item in customers"
									:key="item.id"
									:label="item.customerName"
									:value="item.id">
								</el-option>
							</el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="注册资本" prop="registeredCapital">
                            <el-input-number v-model="formData.registeredCapital" :min="0" placeholder="请输入注册资本（单位：万元）" clearable class="w100"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="员工人数" prop="employeeCount">
                            <el-input-number v-model="formData.employeeCount" :min="0" placeholder="请输入员工人数（单位：人）" clearable class="w100"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="成立日期" prop="enterpriseDate">
                            <el-date-picker v-model="formData.enterpriseDate" placeholder="请选择成立日期" clearable class="w100"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="企业规模" prop="enterpriseScale">
                            <Dictionary v-model="formData.enterpriseScale" dict-label="crm_enterprise_scale" placeholder="请选择企业规模"></Dictionary>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="企业简介" prop="description">
                            <el-input type="textarea" v-model="formData.description" placeholder="请输入企业简介" rows="5" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="备注" prop="remark">
                            <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel">取 消</el-button>
                    <el-button type="primary" @click="onSubmit">保 存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { editCrmEnterprise, getCrmEnterprise } from '/@/api/crm/enterprise/index';
import { getCrmCustomerList } from '/@/api/crm/customer/index';
import Dictionary from '/@/components/dictionary/index.vue';
export default {
    name: 'crm/enterprise/edit',
    components: { Dictionary },
    setup(props, context) {
        const editCrmEnterpriseFormRef = ref();
        const state = reactive({
            isShowDialog: false,
            customers: [],
            formData: {},
            formRules : {
                enterpriseName: { required: true, message: '请输入企业名称', trigger: 'blur' },
                creditCode: { required: true, message: '请输入统一社会信用代码', trigger: 'blur' },
                legalName: { required: true, message: '请输入法定代表人', trigger: 'blur' },
            }
        });

        const loadCustomerData = (query?: string) => {
            if(query == '') return;
            getCrmCustomerList({customerName: query}).then((res) => {
                if(res.code == 0){
                    state.customers = res.data;
                }
            });
        }
        // 打开弹窗
        const openDialog = (row?: object) => {
            state.isShowDialog = true;
            if(row){
                getCrmEnterprise({id: row.id}).then((res) => {
                    if(res.code == 0){
                        state.formData = Object.assign({}, res.data);
                        state.customers = res.data.customers;
                    }
                });
            }
        };
        // 关闭弹窗
        const closeDialog = (row?: object) => {
            state.isShowDialog = false;
            for(var key in state.formData) {
                state.formData[key] = undefined;
            }
            editCrmEnterpriseFormRef.value.resetFields();
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 新增
        const onSubmit = () => {
            editCrmEnterpriseFormRef.value.validate(valid => {
                if(valid){
                    let params = {
                        id: state.formData.id,
                        enterpriseName: state.formData.enterpriseName,
                        creditCode: state.formData.creditCode,
                        legalName: state.formData.legalName,
                        mobile: state.formData.mobile,
                        telephone: state.formData.telephone,
                        email: state.formData.email,
                        website: state.formData.website,
                        address: state.formData.address,
                        registeredCapital: state.formData.registeredCapital,
                        enterpriseDate: state.formData.enterpriseDate,
                        industry: state.formData.industry,
                        enterpriseScale: state.formData.enterpriseScale,
                        employeeCount: state.formData.employeeCount,
                        description: state.formData.description,
                        customerIds: state.formData.customerIds,
                        remark: state.formData.remark,
                    }
                    editCrmEnterprise(params).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('保存成功');
                            context.emit('submit');
                            closeDialog();
                        }
                    });
                }
            });
        };
        return {
            editCrmEnterpriseFormRef,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            loadCustomerData,
            ...toRefs(state),
        };
    },
};
</script>